package com.example.wxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.wxai.mapper.CommentMapper;
import com.example.wxai.mapper.EssayMapper;
import com.example.wxai.mapper.LikesMapper;
import com.example.wxai.mapper.UserMapper;
import com.example.wxai.pijo.dto.LikesDto;
import com.example.wxai.pijo.entity.Comment;
import com.example.wxai.pijo.entity.Essay;
import com.example.wxai.pijo.entity.Likes;
import com.example.wxai.pijo.entity.User;
import com.example.wxai.service.ILikesService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-16
 */
@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements ILikesService {

    @Resource
    private EssayMapper essayMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;
    @Transactional
    @Override
    // TODO: 2024/12/16 防止数据库同时被修改应该加锁

    public void likes(LikesDto likesDto) {
        //1.判断是点赞还是取消点赞
        if(likesDto.isLikes()){
            //点赞
            Likes likes=new Likes();
        BeanUtils.copyProperties(likesDto,likes);
        save(likes);
           //判断是文章点赞还是评论点赞
            if(likesDto.getToCommentId()!=null){
                //评论点赞
               UpdateWrapper<Comment> commentUpdateWrapper=new UpdateWrapper<>();
               commentUpdateWrapper.eq("comment_id",likesDto.getToCommentId()).setSql("comment_like_count=comment_like_count+1");
               commentMapper.update(commentUpdateWrapper);

            }else {
                //修改文章表点赞数据
                UpdateWrapper<Essay> updateWrapper=new UpdateWrapper();
                updateWrapper.eq("essay_id",likesDto.getEssayId()).setSql("essay_likes=essay_likes+1");
                essayMapper.update(updateWrapper);

                //我的点赞数加1
                UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
                userUpdateWrapper.eq("user_id",likesDto.getUserId())
                        .setSql("likes=likes+1");
                userMapper.update(userUpdateWrapper);
            }
        }else {
            //取消点赞
            QueryWrapper<Likes> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("essay_id",likesDto.getEssayId()).eq("user_id",likesDto.getUserId());
            remove(queryWrapper);
            if(likesDto.getToCommentId()!=null){
                //评论点赞
                UpdateWrapper<Comment> commentUpdateWrapper=new UpdateWrapper<>();
                commentUpdateWrapper.eq("comment_id",likesDto.getToCommentId()).setSql("comment_like_count=comment_like_count-1");
                commentMapper.update(commentUpdateWrapper);
            }else {
                //修改文章表点赞数据
                UpdateWrapper<Essay> updateWrapper=new UpdateWrapper();
                updateWrapper.eq("essay_id",likesDto.getEssayId()).setSql("essay_likes=essay_likes-1");
                essayMapper.update(updateWrapper);


                //我的点赞数-1
                UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
                userUpdateWrapper.eq("user_id",likesDto.getUserId())
                        .setSql("likes=likes-1");
                userMapper.update(userUpdateWrapper);
            }
        }
    }

    @Override
    public Likes isLikes(String essayId, String userId) {
        Likes one = lambdaQuery().eq(Likes::getEssayId, essayId).eq(Likes::getUserId, userId).one();
        return one;

    }
}
